#pragma once

#include "resource.h"       // main symbols
#include <atlhost.h>
#include "FrogProgressReportDialog.h"
#include "CGdiPlusBitmap.h"
#include <gdiplus.h>
#include "..\FilesRefactor\FilesRefactor.h"
#include "..\FilesRefactor\PDFTemplates.h"
#include "..\FilerFrogUtils\FilerFrogFilesListSorter.h"

using namespace Gdiplus;

struct PdfAlbumCommandArgs
{
	list<wstring> FilesList;
	int Rows;
	int Cols;
	bool IsLandscape;
	HPDF_PAGESIZES PageSize;
	bool PicBorders;
	bool Optimize;
	HPDF_REAL Margins;
	HPDF_REAL GapPics;
};

class CFrogPdfAlbumDialog: public CFrogProgressReportDialog<CFrogPdfAlbumDialog>,
	public CWinDataExchange<CFrogPdfAlbumDialog>
{
public:
	enum { IDD = IDD_DIALOG_PDF };

	CFrogPdfAlbumDialog(list<wstring> filesList, HICON hIcon);	
	
	virtual ~CFrogPdfAlbumDialog();

	PdfAlbumCommandArgs m_PdfAlbumCommandArgs;

private:
	// For tracking changes
	int m_LastRows;
	int m_LastCols;
	bool m_LastIsLandscape;
	bool m_LastPicBorders;
	HPDF_REAL m_LastMargins;
	HPDF_REAL m_LastGapPics;
	int m_OrderBy;

	Bitmap* m_TempBmp;

	CGdiPlusBitmapResource* m_Image;
	list<Gdiplus::Image*> m_ImageList;

	FilerFrogFilesListSorter m_FilerFrogFilesListSorter;

	void  DrawPreviewControl(bool repaint = false);
	Gdiplus::Image* GetImageAtIndex(int idx);
	void InitImageList();	
	void SortFilesList();

public:
	BEGIN_MSG_MAP(CFrogPdfAlbumDialog)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		MESSAGE_HANDLER(WM_PAINT, OnPaint)
		COMMAND_HANDLER(IDC_EDIT_ROWS, EN_CHANGE, OnEnChangeEditRows)
		COMMAND_HANDLER(IDC_EDIT_COLS, EN_CHANGE, OnEnChangeEditCols)
		COMMAND_HANDLER(IDC_EDIT_GAP, EN_CHANGE, OnEnChangeEditGap)
		COMMAND_HANDLER(IDC_EDIT_MARGIN, EN_CHANGE, OnEnChangeEditMargin)
		COMMAND_HANDLER(IDOK, BN_CLICKED, OnBnClickedOk)
		COMMAND_HANDLER(IDC_CHECK_LANDSCAPE, BN_CLICKED, OnBnClickedCheckLandscape)
		COMMAND_HANDLER(IDC_CHECK_BORDERS, BN_CLICKED, OnBnClickedCheckBorders)
		COMMAND_HANDLER(IDC_RADIO_ORDER_RANDOM, BN_CLICKED, OnRadioOrderBnClicked)
		COMMAND_HANDLER(IDC_RADIO_ORDER_ALPHA, BN_CLICKED, OnRadioOrderBnClicked)
		COMMAND_HANDLER(IDC_RADIO_ORDER_DATE, BN_CLICKED, OnRadioOrderBnClicked)
		// FrogBaseDialog Inherited
		CHAIN_MSG_MAP(CFrogProgressReportDialog)
	END_MSG_MAP()

	BEGIN_DDX_MAP(CFrogPdfAlbumDialog)		
		DDX_RADIO(IDC_RADIO_ORDER_RANDOM, m_OrderBy)
	END_DDX_MAP()

	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnEnChangeEditRows(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnEnChangeEditCols(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnEnChangeEditGap(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnEnChangeEditMargin(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnBnClickedOk(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnBnClickedCheckLandscape(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnBnClickedCheckBorders(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnRadioOrderBnClicked(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	
	virtual void ExecuteCommand();
};